package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.workflow.domain.entity.WorkflowNodeEntity;
import com.xbongbong.workflow.pojo.Data;
import com.xbongbong.workflow.pojo.FlowData;
import com.xbongbong.workflow.pojo.FlowDatas;
import com.xbongbong.workflow.pojo.User;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 工作流上下文转换help
 * @author 魏荣杰
 *
 */
public class WorkflowTransferHelp {

    /**
     * Description: 封装上下文流转中其他节点的数据（追加操作）
     * @param nodeId 节点id
     * @param formId 节点上的formId
     * @param appId 节点上的appId
     * @param menuId 节点上的menuId
     * @param saasMark 节点上的saasMark
     * @param businessType 节点上的businessType
     * @param ids 节点上的数据id
     * @param subDataIds 节点上数据的子表单数据id
     * @param flowDatas 之前节点的流转数据
     * @return com.xbongbong.workflow.pojo.FlowDatas
     * @throws 
     * @author 魏荣杰
     * @date 2021/6/15 15:36
     * @since
     */
    public static FlowDatas formatFlowDatas(Long nodeId, Long formId, Long appId, Long menuId, Integer saasMark, Integer businessType, List<Long> ids, List<Long> subDataIds, FlowDatas flowDatas) {
        Map<String, FlowData> nodeIdMap;
        if (Objects.isNull(flowDatas)) {
            nodeIdMap = new HashMap<>();
        } else {
            nodeIdMap = flowDatas.getNodeIdMap();
        }
        FlowData flowData = new FlowData();
        flowData.setFormId(formId);
        flowData.setAppId(appId);
        flowData.setMenuId(menuId);
        flowData.setSaasMark(saasMark);
        flowData.setBusinessType(businessType);
        flowData.setIds(ids);
        flowData.setSubDataIdIn(subDataIds);

        nodeIdMap.put(nodeId.toString(), flowData);

        flowDatas.setNodeIdMap(nodeIdMap);
        return flowDatas;
    }

    /**
     * Description: 初始化创建WorkflowTransferPOJO
     * @param paasFormDataEntity 触发表数据
     * @param workflowTransferPOJO 已有的上下文数据，如果有进行重新封装，为null则为直接封装一个新的
     * @param flowDatas 节点的数据
     * @param corpid 公司id
     * @param userVO 触发工作流人
     * @param operate
     * @param isImport
     * @param subBusinessType
     * @return com.xbongbong.workflow.pojo.WorkflowTransferPOJO
     * @throws
     * @author 魏荣杰
     * @date 2021/6/15 15:19
     * @since
     */
    public static WorkflowTransferPOJO createTransferData(PaasFormDataEntity paasFormDataEntity, Map<String, FieldAttrEntity> explainMap, WorkflowTransferPOJO workflowTransferPOJO, FlowDatas flowDatas, String corpid, UserVO userVO, WorkflowNodeEntity startNode, Integer operate, Integer isImport, Integer subBusinessType) {
        JSONObject data = paasFormDataEntity.getData();

        // TODO: 2021/6/15 data 数据的处理
        FormDataUtil.setSystemData(data, paasFormDataEntity);
        if (Objects.isNull(workflowTransferPOJO)) {
            Data pojoData = new Data(startNode.getAppId(), startNode.getMenuId(), startNode.getFormId(), paasFormDataEntity.getId(), data, startNode.getBusinessType(), startNode.getSaasMark(), startNode.getId(), subBusinessType);
            workflowTransferPOJO = new WorkflowTransferPOJO();
            workflowTransferPOJO.setData(pojoData);
            User user = new User();
            BeanUtil.copyProperties(userVO, user);
            workflowTransferPOJO.setUser(user);
            workflowTransferPOJO.setCorpid(corpid);
        } else {
            Data pojoData = workflowTransferPOJO.getData();
            pojoData.setData(data);
            workflowTransferPOJO.setData(pojoData);
        }

        if (Objects.nonNull(flowDatas)) {
            workflowTransferPOJO.setFlowDatas(flowDatas);
        }
        workflowTransferPOJO.setOperate(operate);
        workflowTransferPOJO.setIsImport(isImport);

        return workflowTransferPOJO;
    }
}
